.PHONY: all clean clean_obj lib fsimage

CCFLAGS	:= -gstabs -I../ -O0 -nostdlib -nostdinc -fno-builtin # -DU_HEAP_DEBUG

LIBGCC	:= $(shell ${CC} -print-libgcc-file-name)

HEADERS := ulib.h umem.h rpc.h pipe.h pci.h pcireg.h
OBJFILES:= ulib.S.o ulib.o rpc.o pipe.o umem.o entry.S.o entry.o test.o file.o file_server.o ramfs.o elf.o up_manager.o mutex.o input.o pci.o e100.o

USERPROG := hello

all: user user.sym

lib:
	${MAKE} -C ../lib/

# the start address is 0x00000000 according to inc/memlayout.h
user: lib fsimage ${OBJFILES}
	${LD} -N -T user.ld -o $@ ${OBJFILES} ../lib/*.o ${LIBGCC}

user.sym: user
	nm $< | grep -i " t " | sed "s/ [tT] / /g" > $@

entry.S.o: entry.S
	${CC} ${CCFLAGS} $< -c -o$@

ulib.S.o: ulib.S
	${CC} ${CCFLAGS} $< -c -o$@

fsimage.S.o: fsimage.S
	${CC} ${CCFLAGS} fsimage.S -c -o$@


%.o: %.c ${HEADERS}
	${CC} ${CCFLAGS} $< -c -o$@

fsimage: ${USERPROG}
	-rm fsimage
	cp ${USERPROG} userroot/
	../utils/ramfstools/ramfstools < mkfsimage
	../utils/bin2S/bin2S fsimage fsimage.data.S

hello: hello.c
	${CC} ${CCFLAGS} $< -o$@

clean_obj:
	-${RM} ${OBJFILES}

clean: clean_obj
	-${RM} user.sym
	-${RM} user

